import {
	login,
	getInfo
} from "@/api/user.js"
import storage from "@/util/storage.js"
import config from "@/common/config.js"

export default {
	state: {
		user: storage.getItem(config.USER) || {},
	},
	getters: {
		getUser: state => storage.getItem(config.USER),
	},
	actions: {
		login(context, data) {
				
			return new Promise(async (resolve, reject) => {
				const res = await login(data)
				if (res.code == 200) {
					storage.setItem(config.TOKEN, res.token)
					storage.setItem(config.LOGINTIME, new Date().getTime())
					storage.setItem(config.TOKENEXPIRES, res.tokenexpires)
					context.dispatch('getUserInfo')
				}
				resolve(res)
			})
		},
		getUserInfo() {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
						storage.setItem(config.USER, res.user)
						//如果需要角色,释放此处便可
						// storage.setItem(config.ROLE, res.roles)
					})
			})
				}

			}
		}
